﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HK3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private HK3DataContext dc = new HK3DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlMovie.DataSource = dc.Movies.ToList();
                ddlMovie.DataValueField = "id";
                ddlMovie.DataTextField = "name";
                ddlMovie.DataBind();
            }
        }

        protected void ddlMovie_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDay.Items.Clear();
            ddlDay.Items.Add(new ListItem("--Select Day--", ""));
            ddlShift.Items.Clear();
            ddlShift.Items.Add(new ListItem("--Select Shift--", "")); 
            try
            {
                int idmovie = Convert.ToInt32(ddlMovie.SelectedValue.ToString());
                ddlDay.DataSource = dc.Schedules.Where(m => m.idMovie == idmovie).Select(m => m.date).ToList();
                ddlDay.DataValueField = "date";
                ddlDay.DataTextField = "date";
                ddlDay.DataBind();
                if (ddlDay.Items.Count >= 1) 
                {
                    ddlDay.Enabled = true;
                }
                else
                {
                    ddlDay.Enabled = false;
                    ddlShift.Enabled = false;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void ddlDay_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlShift.Items.Clear();
            ddlShift.Items.Add(new ListItem("--Select Shift--", ""));
            int idmovie = Convert.ToInt32(ddlMovie.SelectedValue.ToString());
            DateTime day = Convert.ToDateTime(ddlDay.SelectedValue.ToString());
            //int idshift = dc.Shifts.Join(dc.Schedules, d=>d.id, d=>d.idShift,);

            ddlShift.DataSource = dc.Schedules.Where(m => m.idMovie == idmovie && m.date.Equals(day)).Select(d=>d.idShift).ToList();
            //.Select(m => m.idShift).ToList();
            //ddlShift.DataValueField = "idShift";
            //ddlShift.DataTextField = "idShift";
            //ddlShift.DataBind();
        }

    }
}